package com.quasar.hpatient.module.comm_splash;

import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.WindowManager;

import com.quasar.hpatient.R;
import com.quasar.hpatient.module.comm_compile.CompileActivity;
import com.quasar.hpatient.module.comm_guide.GuideActivity;
import com.quasar.hpatient.module.comm_login.LoginActivity;
import com.quasar.hpatient.module.comm_main.MainActivity;

import java.util.List;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.context.BaseConstant;
import lib.quasar.db.manager.DBManager;
import lib.quasar.db.table.User;
import lib.quasar.permission.annotation.PermissionAgain;
import lib.quasar.permission.annotation.PermissionDenied;
import lib.quasar.permission.annotation.PermissionFail;
import lib.quasar.permission.annotation.PermissionSucc;
import lib.quasar.permission.core.PermissionManager;
import lib.quasar.permission.core.intent.IntentType;
import lib.quasar.util.ToastUtil;
import lib.quasar.widget.timer.TimerView;

/**
 * description: 闪屏, 需要获取SD卡写入权限
 * created by kalu on 2018/4/4 10:33
 */
public class SplashActivity extends BaseActivity<SplashPresenter> implements SplashView {

    @Override
    public int initView() {
        return R.layout.activity_comm_splash;
    }

    @Override
    protected boolean setFull() {
        return true;
    }

    @Override
    public void initDataLocal() {
        PermissionManager.get(SplashActivity.this)
                .setPermissionName(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .setPageType(IntentType.GOOGLE_SETTING)
                .setRequestCode(BaseConstant.PERMISSION_SD)
                .request();
    }

    @Override
    public void initDataNet() {
    }

    @PermissionSucc(BaseConstant.PERMISSION_SD)
    public void cardSucc(int code, List<String> list) {

        final TimerView time = findViewById(R.id.activity_splash_time);
        if (null == time) return;

        time.setOnCountdownChangeListener(() -> {
            User config = DBManager.getInstance().syncGetUserModel();
            boolean isGuide = (null == config || config.needGuide());
            boolean isLogin = (null == config || config.needLogin());
            boolean isCompile = false;

            if (isGuide) {
                goNext(GuideActivity.class);
            } else if (isLogin) {
                goNext(LoginActivity.class);
            } else if (isCompile) {
                goNext(CompileActivity.class);
            } else {
                goNext(MainActivity.class);
            }
            onBackPressed();
        });
        time.startTimer();
    }

    @PermissionFail(BaseConstant.PERMISSION_SD)
    public void cardFail(int code, List<String> list) {
        ToastUtil.showToast("获取sd卡读写权限失败");
        onBackPressed();
    }

    @PermissionAgain(BaseConstant.PERMISSION_SD)
    public void cardAgain(int code, List<String> list) {
    }

    @PermissionDenied(BaseConstant.PERMISSION_SD)
    public void cardDenied(int code, List<String> list, final Intent intent) {
        goNext(intent);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionManager.onRequestPermissionsResult(SplashActivity.this, requestCode, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}